// pages/user/wallet.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      cash:0,
      user_money:0,
      daozhang:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.money);
    if(options.money == undefined){
      wx.showToast({
        title: '请求非法',
        icon: 'success',
        duration: 2000
      });
      setTimeout(function () {
        wx.navigateBack({
          delta: 1//默认值是1，返回的页面数，如果 delta 大于现有页面数，则返回到首页。
        });
      }, 2000);
      return;
    }

    this.setData({
      user_money: options.money,
      bank: options.bank
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tixian_choose:function(e){
    var that = this;
    var money = e.target.dataset.money;
    if (money == 50) {
      that.setData({
        tixian50: true,
        tixian100: false,
        tixian500: false,
        tixian1000: false,
      })
    }
    if (money == 100) {
      that.setData({
        tixian50: false,
        tixian100: true,
        tixian500: false,
        tixian1000: false,
      })
    }
    if (money == 500) {
      that.setData({
        tixian50: false,
        tixian100: false,
        tixian500: true,
        tixian1000: false,
      })
    }
    if (money == 1000) {
      that.setData({
        tixian50: false,
        tixian100: false,
        tixian500: false,
        tixian1000: true,
      })
    }
    that.setData({
      'cash':money,
      'daozhang':money - money * 0.05,
    })
  },
  tixian:function(){
    var that = this;

    if (that.data.cash == 0){
      wx.showToast({
        title: '请选择提现金额',
        duration: 2000
      });
      return;
    }
    if (parseInt(that.data.user_money) < that.data.cash){
      wx.showToast({
        title: '余额不足',
        duration: 2000
      });
      return;
    }

    wx.request({
      url: app.d.hostUrl + 'wapi/transfers.html',
      method: 'post',
      data: {
        num: that.data.cash,
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var data = res.data;
        if (data.status == 0) {
          var remain = that.data.user_money - that.data.cash;
          that.setData({
            user_money:remain,
          })
          wx.showToast({
            title: '提现成功',
            duration: 2000
          });
        } else {
          wx.showToast({
            title: data.msg,
            duration: 2000
          });
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },
  onShareAppMessage: function () {
    return {
      title: '赚钱那些事儿',
      path: '/pages/index/index?scene=invite%3D' + app.globalData.user_id,
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          duration: 2000
        });
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },
})